<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="equipName">
              <el-input v-model="filters.equipName" clearable placeholder="接收器名称"></el-input>
            </el-form-item>
            <el-form-item prop="equipCode">
              <el-input v-model="filters.equipCode" clearable placeholder="接收器编号"></el-input>
            </el-form-item>
            <el-form-item prop="equipMac">
              <el-input v-model="filters.equipMac" clearable placeholder="MAC地址"></el-input>
            </el-form-item>
            <el-form-item prop="businessId">
              <el-select v-model="filters.businessId" clearable placeholder="商户" filterable>
                <el-option v-for="item in businessList" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="equipState">
              <el-select placeholder="接收器状态" v-model="filters.equipState" clearable filterable>
                <el-option :value="1" label="待连接"></el-option>
                <el-option :value="2" label="在线"></el-option>
                <el-option :value="3" label="离线"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column width="200" label="接收器名称" align="center" prop="equipName"> </el-table-column>
        <el-table-column width="200" label="接收器编号" align="center" prop="equipCode"> </el-table-column>
        <el-table-column align="center" label="MAC地址" width="200" prop="equipMac"> </el-table-column>
        <el-table-column align="center" width="200" label="商户" prop="businessName"> </el-table-column>
        <el-table-column align="center" label="接收器状态" width="110px">
          <template slot-scope="scope">
            <el-tag size="mini" effect="dark" :type="formatReceiverStates(scope.row.equipState)">
              {{ formatReceiverState(scope.row.equipState) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="创建人" prop="createUserName"> </el-table-column>
        <el-table-column align="center" width="200" label="创建时间" prop="createDate"> </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
                <el-dropdown-item v-has="permissionHeartbeat" @click.native="handleHeartbeatRecord(scope.row)">心跳记录</el-dropdown-item>
                <el-dropdown-item v-has="permissionAlarm" @click.native="hanldeAlarmLogRecord(scope.row)">接收记录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>

<script>
import BusinessApi from '@/api/business/business';
import ReceiverApi from '@/api/equip/receiver';
import * as message from '@/constants/message';
export default {
  name: 'ReceiverManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      businessList: [],
      listLoading: true,

      filters: {
        equipName: '',
        equipCode: '',
        businessId: '',
        equipMac: '',
        equipState: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_EQUIP_RECEIVER_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_RECEIVER_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_RECEIVER_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_RECEIVER_LIST_DELETE',
      // permissionReceiver:'SYSTEM_MANAGER_EQUIP_RECEIVER_LIST_BEEPER',
      permissionHeartbeat: 'SYSTEM_MANAGER_EQUIP_RECEIVER_LIST_HEARTBEAT',
      permissionAlarm: 'SYSTEM_MANAGER_EQUIP_RECEIVER_LIST_ALARM',
      // permissionUpStatus:'SYSTEM_MANAGER_EQUIP_RECEIVER_LIST_STATE',
    };
  },
  components: {},
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listBusiness();
      _this.listData();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },



    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    formatReceiverState(cellValue) {
      if (cellValue === 0) {
        return '未开通';
      } else if (cellValue === 1) {
        return '未激活';
      } else if (cellValue === 2) {
        return '在线';
      } else if (cellValue == 3) {
        return '离线';
      }
    },
    formatReceiverStates(cellValue) {
      if (cellValue === 0) {
        return 'danger';
      } else if (cellValue === 1) {
        return '';
      } else if (cellValue === 2) {
        return 'success';
      } else if (cellValue === 3) {
        return 'info';
      }
    },
    listBusiness() {
      BusinessApi.listIdNameVo().then((res) => {
        this.businessList = res.data;
      });
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        equipName: _this.filters.equipName,
        equipCode: _this.filters.equipCode,
        equipMac: _this.filters.equipMac,
        businessId: _this.filters.businessId,
        equipState: _this.filters.equipState,
      };
      // 发送请求
      ReceiverApi.listReceiver(cond)
        .then((res) => {
          _this.listLoading = false;
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该接收器', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.receiverId,
          state: -1,
        };
        ReceiverApi.deleteReceiver(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/receiverDetail',
        query: { id: row.receiverId },
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/equip/receiverEdit',
        query: { id: val.receiverId },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/equip/receiverAdd');
    },
    handleHeartbeatRecord(val) {
      // 调用心跳记录
      let _this = this;
      _this.$router.push({
        path: '/equip/receiverDetail',
        query: { id: val.receiverId, recordType: 'heartBeat' },
      });
    },
    // 调用报警记录列表
    hanldeAlarmLogRecord(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/receiverDetail',
        query: { id: row.receiverId, recordType: 'alarm' },
      });
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
